package leetcode.array;

import java.util.ArrayList;
import java.util.List;

/**
 * 303. Range Sum Query - Immutable
 * https://leetcode.com/problems/range-sum-query-immutable/
 */

public class NumArray {

    private List<Integer> sumList = null;

    public NumArray(int[] nums) {
        this.sumList = new ArrayList<>();
        int sum = 0;
        if (nums.length < 1) {
            return;
        }
        for (int i = 0; i < nums.length; i++) {
            sum += nums[i];
            sumList.add(sum);
        }
        for (Integer i: this.sumList) {
            System.out.print(i+" ");
        }
        System.out.println();
    }

    public int sumRange(int i, int j) {
        if (null != this.sumList) {
            int tmp = i - 1;
            System.out.println("tmp="+tmp);
            if (tmp >= 0) {
                return this.sumList.get(j) - this.sumList.get(tmp);
            } else {
                return this.sumList.get(j);
            }
        }
        return 0;
    }

}
